package com.hll.javaweb.servlet.mvc.controller;

import com.alibaba.fastjson.JSON;
import com.hll.javaweb.servlet.mvc.service.StudentService;
import com.hll.javaweb.servlet.mvc.service.impl.StudentServiceImpl;
import com.hll.javaweb.servlet.reflect_servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author hll
 * @create 2022-05-10 17:42
 */
@WebServlet("/students")
public class StudentController extends BaseServlet {
  private static final long serialVersionUID = -826372451127815355L;
  private final StudentService studentService = new StudentServiceImpl();

  public void addStudent(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    String name = request.getParameter("name");
    String school = request.getParameter("school");
    if (studentService.addStudent(name, school)) {
      response.getWriter().write("success");
    } else {
      response.getWriter().write("fail");
    }
  }

  public void getStudents(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    response.getWriter().write(JSON.toJSONString(studentService.getStudents()));
  }
}
